package com.hundsun.documentripper.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hundsun.documentripper.domain.dto.BaseResponse;
import com.hundsun.documentripper.domain.dto.DemoSource;
import com.hundsun.documentripper.domain.pojo.Demo;
import com.hundsun.documentripper.domain.query.DemoQuery;
import com.hundsun.documentripper.error.ErrorCatcher;
import com.hundsun.documentripper.service.DemoService;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：预设模板控制层
 * @date ：Created in 2021/1/26
 */
@Api("预设模板")
@RestController
public class DemoController {

    @Autowired
    private DemoService demoService;

    @ApiOperation("新增预设模板")
    @PostMapping("/demo")
    @ErrorCatcher(code = "7001")
    public BaseResponse<Demo> insertDemo(@RequestBody Demo demo, HttpServletRequest request) {
        demo.setCreateUser((String) request.getSession().getAttribute("userUuid"));
        return new BaseResponse<Demo>(demoService.insertDemo(demo));
    }

    @ApiOperation("更新预设模板")
    @PutMapping("/demo")
    @ErrorCatcher(code = "7002")
    public BaseResponse<Void> updateDemo(@RequestBody Demo demo, HttpServletRequest request) {
        demo.setModifyUser((String) request.getSession().getAttribute("userUuid"));
        demoService.updateDemo(demo);
        return new BaseResponse<Void>();
    }

    @ApiOperation("根据唯一标识删除预设模板")
    @DeleteMapping("/demo/{uuid}")
    @ErrorCatcher(code = "7003")
    public BaseResponse<Void> deleteDemo(@PathVariable("uuid") String uuid) {
        demoService.deleteDemo(uuid);
        return new BaseResponse<Void>();
    }

    @ApiOperation("分页获取预设模板列表")
    @PostMapping("/demo/all")
    @ErrorCatcher(code = "7004")
    public BaseResponse<IPage<Demo>> getDemoListByPage(@RequestBody DemoQuery demoQuery) {
        return new BaseResponse<IPage<Demo>>(demoService.getDemoListByPage(demoQuery));
    }

    @ApiOperation("获取预设模板文件元数据")
    @GetMapping("/demo/{uuid}/source")
    @ErrorCatcher(code = "7005")
    public BaseResponse<DemoSource> getDemoSourceById(@PathVariable("uuid") String uuid) {
        return new BaseResponse<DemoSource>(demoService.getDemoSourceById(uuid));
    }

    @ApiOperation("保存获取预设模板文件元数据")
    @PostMapping("/demo/source")
    @ErrorCatcher(code = "7006")
    @Transactional
    public BaseResponse<DemoSource> saveDemoSource(@RequestBody DemoSource fileSource, HttpServletRequest request) {
        fileSource.setCreateUser((String) request.getSession().getAttribute("userUuid"));
        return new BaseResponse<DemoSource>(demoService.saveDemoSource(fileSource));
    }

}